package net.db.impl;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.db.BaseDAO;

/**
 * 注入
 * @author ALLEN
 *
 */
public class DBSInfuse extends BaseDAO{
	
	/**
	 * 连接
	 * @param openTransaction T:用于插入多条数据
	 * @throws SQLException 
	 */
	private void getConnection(boolean openTransaction) throws SQLException {
		if(conn != null) return;
		conn = openConnection();
		if (openTransaction) {
			conn.setAutoCommit(false); //是否自动提交
		}
	}
	
	/**
	 * 录入(非站位)
	 * @param sql
	 * @throws SQLException 
	 */
	public void execute(String sql) throws SQLException{
		getConnection(true); //不必插入多条
		if(stmt == null){
			stmt = conn.createStatement();
		}
		stmt.addBatch(sql);
	}
	
	
	/**
	 * 录入(站位方式)
	 * @param sql
	 * @param val
	 * @throws SQLException 
	 */
	public void execute(String sql,Object [] val) throws SQLException{
		getConnection(true);
		if(ps == null){
			ps = conn.prepareStatement(sql);
		}
		for(int i = 0 ;i < val.length; i++){
			ps.setObject(i+1, val[i]);
		}
		ps.addBatch();
	}
	
	/**
	 * 查询
	 * @param sql
	 * @param isClear 清空批处理数据
	 * @return
	 * @throws SQLException 
	 */
	public <T> T select(String sql,boolean isClear) throws SQLException{
		getConnection(true);
		if(isClear) clear(); //提交
		ps = conn.prepareStatement(sql);
		rs = ps.executeQuery();
		ResultSetMetaData rsmd = rs.getMetaData();
		int count = rsmd.getColumnCount();
		String name;
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map;
		while(rs.next()){
			map = new HashMap<String, Object>();
			for(int i = 1 ;i <= count ; i++){
				name = rsmd.getColumnLabel(i);
				map.put(name, rs.getObject(name));
			}
			list.add(map);
		}
		return (T)list;
	}
	
	/**
	 * 查询(站位)
	 * @param sql
	 * @param vals
	 * @param isClear
	 * @return
	 * @throws SQLException
	 */
	public <T> T select(String sql, Object [] vals, boolean isClear) throws SQLException{
		getConnection(true);
		if(isClear) clear(); //提交
		if(ps == null){
			ps = conn.prepareStatement(sql);
		}
		for(int i = 0 ;i < vals.length; i++){
			ps.setObject(i+1, vals[i]);
		}
		rs = ps.executeQuery();
		ResultSetMetaData rsmd = rs.getMetaData();
		int count = rsmd.getColumnCount();
		String name;
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map;
		while(rs.next()){
			map = new HashMap<String, Object>();
			for(int i = 1 ;i <= count ; i++){
				name = rsmd.getColumnLabel(i);
				map.put(name, rs.getObject(name));
			}
			list.add(map);
		}
		return (T)list;
	}

	/**
	 * 录入数据库 并 清除缓存
	 */
	public void clear(){
		try {
			if(ps != null){
				ps.executeBatch();
				ps.clearBatch();
				ps = null;
			}
			if(stmt != null){
				stmt.executeBatch();
				stmt.clearBatch();
				stmt = null;
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 提交
	 */
	public void commit() {
		try {
			if(conn == null) return;
			if(ps != null || stmt!= null) clear();
			conn.commit();
			close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 回调
	 */
	public void rollback() {
		try {
			conn.rollback();
			close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
